package com.snj.test2510;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PlaceRouteAddUtils {


    /**
     * 从 PlaceRoute 对象中扣除指定地点及其对应的值
     *
     * @param placeRoute  包含 placeWay、stowage 和 totalLoad 的 PlaceRoute 对象
     * @param removePlace 需要扣除的地点
     * @return 更新后的 PlaceRoute 对象
     */
    public static String placeAndStowageAndTotalLoad(PlaceRoute placeRoute, String removePlace, Double routeNum) {
        if (placeRoute == null || removePlace == null) {
            // 如果输入为空，直接返回原始对象
//            throw new CommonException(Constants.ERROR, "数据异常");
        }

        String placeWay = placeRoute.getPlaceWay();
        String stowage = placeRoute.getStowage();

        // 将 placeWay 按 "->" 拆分为数组
        String[] places = placeWay.split("->");

        // 排除首尾的 "河西基地" 和 "仓储缓存点"，得到中间的地点数组
        List<String> middlePlaces = new ArrayList<>();
        for (int i = 1; i < places.length - 1; i++) {
            middlePlaces.add(places[i].trim());
        }

        // 将 stowage 按 "," 拆分为数组
        String[] stowages = stowage.split(",");
        List<String> stowageList = new ArrayList<>(Arrays.asList(stowages));

        List<String> teamList = new ArrayList<>();
        for (int i = 0; i < middlePlaces.size(); i++) {
            String place = middlePlaces.get(i);
            Double value = Double.valueOf(stowageList.get(i));
            if (place.equals(removePlace)) {
                Double teamValue = 0.0;
                if (value > routeNum) {
                    Double info = value - routeNum;
                    teamValue += info;
                }
                // 使用 String.format 保留 5 位小数
                String formattedValue = String.format("%.5f", teamValue);
                teamList.add(formattedValue);
            } else {
                String formattedValue = String.format("%.5f", value);
                teamList.add(formattedValue);
            }
        }

        // 重新拼接 stowage
        String newStowage = String.join(", ", teamList);

        return newStowage;
    }

    public static void main(String[] args) {
        // 示例数据
        PlaceRoute placeRoute = new PlaceRoute(
                "河西基地->南通本部->如东->启东->仓储缓存点",
                "0.1687, 1.4482, 2.9738",
                4.59070
        );
        String removePlace = "如东";

//        // 调用方法扣除指定地点及其对应的值
        String result = placeAndStowageAndTotalLoad(placeRoute, removePlace, 1.2);

        // 输出结果
        System.out.println("原始 placeWay: " + placeRoute.getPlaceWay());
        System.out.println("原始 stowage: " + placeRoute.getStowage());
        System.out.println("原始 totalLoad: " + placeRoute.getTotalLoad());
        System.out.println("扣除 " + removePlace + " 后的 placeWay: " + result);
        System.out.println("扣除 " + removePlace + " 后的 stowage: " + result);
        System.out.println("扣除 " + removePlace + " 后的 totalLoad: " + result);
    }
}

